<style>

</style>

<template>
    <div class="">
        <Form ref="formValidate" :model="formValidate" :rule="rule" :label-width="80">
            <FormItem label="课程名称">
                <p>{{formValidate.course}}</p>
            </FormItem>
            <FormItem label="问题">
                <p>{{formValidate.question}}</p>
            </FormItem>
            <FormItem label="回答">
                <p>{{formValidate.answer}}</p>
            </FormItem>
            <FormItem label="评论" prop='comment'>
                <Input type="textarea" v-model="formValidate.content" :rows="4" placeholder="请发表评论内容"></Input>
            </FormItem>
            <FormItem>
                <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
            </FormItem>
        </Form>
    </div>
</template>

<script>
import http from '../../../libs/http';
export default {
    data () {
        return {
            formValidate: {
                course: '',
                question: '',
                answer: '',
                content: '',
            },
            rule: {
                comment: [{require: true, message: '评论不能为空', trigger: 'blur'}]
            },
        }
    },
    methods: {
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {
                    //提交后台
                    var data = {
                        content: this.formValidate.content,
                        answer_id: this.formValidate.id,
                        to_uid: this.formValidate.user_id
                    }
                    this.apiPost('admin/course/answer/comment', data).then((res) => {
                        if (res.code != 200) {
                            this.handleError(res);
                        } else {
                            this.$emit('closeResponse');
                            this.$Message.success('回复成功');
                            this.handleReset(name);
                        }
                    })
                } else {
                    this.$Message.error('表单验证失败!');
                }
            })
        },
        handleReset (name) {
            this.$refs[name].resetFields();
        },
        getQuestion (id) {  //获取问题详情
            var that = this;
            var url = 'admin/course/answer/read/' + id;
            this.apiGet(url).then((res) => {
                if (res.code != 200) {
                    this.handleError(res);
                } else {
                    that.formValidate = res.data;
                }
            });
        }
    },
    mixins: [http]
}
</script>